<?php
/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

// array for JSON response
$response = array();
 

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for required fields
if (isset($_POST['TEN_DIA_DIEM']) && isset($_POST['TEN_MON_AN']) && isset($_POST['DIA_CHI']) && 
		isset($_POST['THANH_PHO']) && isset($_POST['uid']) && 
		isset($_POST['LATITUDE']) && isset($_POST['LONGITUDE']) && isset($_POST['LOAI_MON_AN_id']) ) {
	
 	$placename = $_POST['TEN_DIA_DIEM'];
    $foodname = $_POST['TEN_MON_AN'];
    $address= $_POST['DIA_CHI'];
    $city = $_POST['THANH_PHO'];
    //$city = utf8_decode($city);
    $phone = $_POST['SO_DIEN_THOAI'];
    $description = $_POST['MO_TA'];
    $latitude = $_POST['LATITUDE'];
    $longitude = $_POST['LONGITUDE'];
    $uid = $_POST['uid'];
    $type_food_id = $_POST['LOAI_MON_AN_id'];
    $quan_huyen = $_POST['QUAN_HUYEN'];

    
   
    $query = mysql_query("SELECT id FROM USERS WHERE unique_id = '".$uid."' ");
    if(mysql_num_rows($query) > 0) {
    	$row = mysql_fetch_array($query);
    	$id = $row['id'];
    	
    	// Search nearby
    	$radius = 10; // kilometers
    	mysql_query("set names 'utf8'");
    	$sql = mysql_query("SELECT id, TEN_DIA_DIEM, TEN_MON_AN, ( 6371 * acos( cos( radians( '$latitude' ) ) *
    			cos( radians( LATITUDE ) ) * cos( radians( LONGITUDE ) - radians('$longitude') ) +
    			sin( radians( $latitude ) ) * sin( radians( LATITUDE ) ) ) )
    			AS distance FROM MON_AN HAVING distance < '$radius' ORDER BY distance LIMIT 0 , 20")
    			or die("error");
    	$nearby_check = mysql_num_rows($sql);
    	if ($nearby_check > 0){
    		if(isset($_POST['confirm']) && $_POST['confirm'] == true){
    			mysql_query("set names 'utf8'");
    			// mysql inserting a new row
    			$result = mysql_query("INSERT INTO MON_AN(TEN_DIA_DIEM, TEN_MON_AN, DIA_CHI, THANH_PHO, SO_DIEN_THOAI, MO_TA, LATITUDE, LONGITUDE,
    					USERS_id, LOAI_MON_AN_id, QUAN_HUYEN)
    					VALUES('$placename', '$foodname', '$address', '$city', 
    						'$phone', '$description', '$latitude', '$longitude', 
    						'$id', '$type_food_id', '$quan_huyen' )");
    			$idFood = mysql_insert_id();
    			// check if row inserted or not
    			if ($result) {
    				// successfully inserted into database
    				$response["success"] = 1;
    				$response["message"] = "Food successfully created.";
    				$response["idFood"] = $idFood;
    			
    				// echoing JSON response
    				echo json_encode($response);
    			} else {
    			
    				// failed to insert row
    				$response["success"] = 0;
    				$response["message"] = "Oops! An error occurred.";
    				$response["xxxxx"] = $type_food_id;
    				// echoing JSON response
    				echo json_encode($response);
    			}
    		} else {
	    		$response["success"] = 2;
	    		$response["message"] = "Duplicate locations";
	    	
	    		$response["places"] = array();
	    	
	    		while ($row = mysql_fetch_array($sql)){
	    			//$place = array();
	    			$place = $row["TEN_DIA_DIEM"];
	    			//$place["distance"] = $row["distance"];
	    			array_push($response["places"], $place);
	    		}
	    		//$response["places"] = array_unique($response["places"]);
	    			
	    		echo json_encode($response);
    		}
    	} else {
    		mysql_query("set names 'utf8'");
    		// mysql inserting a new row
    		$result = mysql_query("INSERT INTO MON_AN(TEN_DIA_DIEM, TEN_MON_AN, DIA_CHI, THANH_PHO, SO_DIEN_THOAI, MO_TA, LATITUDE, LONGITUDE,
    				USERS_id, LOAI_MON_AN_id, QUAN_HUYEN)
    				VALUES('$placename', '$foodname', '$address', '$city', 
    					'$phone', '$description', '$latitude', '$longitude', 
    					'$id', '$type_food_id', '$quan_huyen' )");
    		$idFood = mysql_insert_id();
    		// check if row inserted or not
    		if ($result) {
    			// successfully inserted into database
    			$response["success"] = 1;
    			$response["message"] = "Food successfully created.";
    			$response["idFood"] = $idFood;
    		
    			// echoing JSON response
    			echo json_encode($response);
    		} else {
    		
    			// failed to insert row
    			$response["success"] = 0;
    			$response["message"] = "Oops! An error occurred.";
    			// echoing JSON response
    			echo json_encode($response);
    		}
    	}
    	
	   
 	} else {
 		$response["success"] = 0;
 		$response["message"] = "UID fail!";
 		
 		echo json_encode($response);
 	} 
   
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
} 
